<template>
  <div>
    <el-form>
      <el-form-item label="角色名称">
        <el-input v-model="rolel.name"></el-input>
      </el-form-item>
      <el-form-item label="角色编码">
        <el-input v-model="rolel.code"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="rolel.description" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="onCancel"
        >取消</el-button>
        <el-button type="primary"
          @click="onSubmit"
        >确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createOrEdit, getRoleById } from '@/services/role'
export default {
  name: 'CreateOrEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: Object
    }
  },
  data () {
    return {
      role: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.loadRole()
    }
  },
  methods: {
    async onSubmit () {
      const { data } = await createOrEdit(this.role)
      if (data.code === '000000') {
        this.$message.success('添加成功')
        this.$emit('success')
        this.role = {}
      }
    },
    onCanel () {
      this.$emit('cancel')
      this.role = {}
    },
    async loadRole () {
      const { data } = await getRoleById(this.roleId)
      if (data.code === '000000') {
        this.role = data.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
